package com.sjq.project.web;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.dao.DeptDao;
import com.sjq.common.dao.ItemDao;
import com.sjq.common.entity.LoginAccount;
import com.sjq.common.util.PageResults;
import com.sjq.common.web.SessionUserHelper;
import com.sjq.project.dao.ProjectDao;
import com.sjq.project.entity.Project;
import com.sjq.project.service.ProjectService;

@Controller
public class ProjectController {
	@Autowired
    private ProjectService projectService;
	@Autowired
    private ProjectDao projectDao;
	@Autowired
    private ItemDao itemDao;
	@Autowired
    private DeptDao deptDao;
    @Autowired
    private HttpSession session;  
    private static final String INITURL = "project/"; 
   /**查询列表***/
   @RequestMapping(INITURL+"ProjectList")
   @ResponseBody
   public JSONObject  ProjectList(Integer pageNo, Integer pageSize, Project project){
 	  if(project==null){
    		project=new Project();
    	}
   	   pageNo = pageNo == null ? 1 : pageNo;
       PageResults<?> pageResults=projectService.findProjectByPage(pageNo, PageResults.ALLPAGESIZE, project);
       JSONObject json=new JSONObject();
       json.put("dataList", pageResults.getResults());
       json.put("pageNo",  pageResults.getPageNo());
       json.put("pages", pageResults.getPageCount());
       return json;
   }
   
   @RequestMapping(INITURL+"initProjectAdd")
   public ModelAndView initProjectAdd() {
	   ModelAndView mav=new ModelAndView();
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
	   LoginAccount la= helper.getLoginAccount();
		mav.setViewName("/view/project/projectAdd");
        mav.addObject("xxzjbh",projectDao.buildProjectId());
        mav.addObject("xmjsyjdm",itemDao.findItemByListAll("ZDY_XMJSYJ"));//
        mav.addObject("sbdw",deptDao.initDeptTreeByPid("", "321300000000"));//
        mav.addObject("xmxz",itemDao.findItemByListAll("ZDY_XMXZ"));//项目性质
        mav.addObject("zjly",itemDao.findItemByListAll("ZDY_ZJLY"));//
        mav.addObject("xmly",itemDao.findItemByListAll("ZDY_XMLY"));//
        mav.addObject("jsnd",itemDao.findItemByListAll("ZDY_JSND"));//
        mav.addObject("xmlb",itemDao.findItemByListAll("ZDY_XMLB"));//
    	mav.addObject("","");
    	return mav;
   }
 
    /**新增项目**/
    @RequestMapping(INITURL+"addProject")
	@ResponseBody
	 public JSONObject addProject(Project project) {
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       return projectService.addProject(project, la);
    }

    
    /**删除信息**/
     @RequestMapping(INITURL+"deleteProject")
	 @ResponseBody
	 public JSONObject deleteProject(String projectId) {
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount la= helper.getLoginAccount();
        return projectService.deleteProject(projectId,la);
    }	
    
    
    /**更新表单**/
     @RequestMapping(INITURL+"motifyProject")
	 @ResponseBody
	 public JSONObject motifyProject(Project project) {
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount la= helper.getLoginAccount();
        return  projectService.updateProject(project,la);   
    }	
    
    
    /**
    * @description 根据查询条件查询表的所有详细信息
    **/
	@RequestMapping(INITURL+"findProjectAllList")
    @ResponseBody
    public JSONObject  findProjectAllList(Project project){
 		   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       	   LoginAccount la= helper.getLoginAccount();
		   return  projectService.findProjectByListAll(project,la);  	
     }
   
   
}
